package com.cndw.kungfu.model.union;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.cndw.kungfu.domain.Player;
import com.cndw.kungfu.domain.Union;
import com.cndw.kungfu.domain.UnionConfig;
import com.cndw.kungfu.domain.UnionMember;
import com.cndw.kungfu.domain.UnionSkillConfig;
import com.cndw.kungfu.model.Context;

@Service
public class UnionServer {

	@Resource
	private UnionManager unionManager;

	public Union getUnionByPlayerId(long playerId) {

		Player player = Context.getPlayerServer().getByPlayerId(playerId);

		if (player == null || player.getUnionId() <= 0) {
			return null;
		}

		return getUnionById(player.getUnionId());
	}

	public Union getUnionById(int unionId) {
		if (unionId <=0 ) {
			return null;
		}
		if (!Context.getDataServer().getUnionMap().containsKey(unionId)) {
			Union union = unionManager.getUnionById(unionId);
			if (union != null) {
				Context.getDataServer().getUnionMap().putIfAbsent(union.getId(), union);
			}			
		}
		return Context.getDataServer().getUnionMap().get(unionId);
	}

	public List<UnionMember> getUnionMemberByUnionId(int unionId) {
		return unionManager.getUnionMemberByUnionId(unionId);
	}

	public boolean checkPermission(long playerId, int permession) {
		return unionManager.checkPermission(playerId, permession);
	}

	public UnionConfig getUnionConfigByLevel(int level) {
		return unionManager.getUnionConfigByLevel(level);
	}

	public List<Integer> getMaintainUnionIds(int time) {
		return unionManager.getMaintainUnionIdsDB(time);
	}

	public UnionSkillConfig getUnionSkillConfig(String key) {
		return unionManager.getUnionSkillConfig(key);
	}

	public void updateUnionAsset(Union union) {
		unionManager.updateUnionAsset(union);
	}

	public void updateMemberResource(UnionMember member) {
		unionManager.updateMemberResource(member);
	}

	public List<Long> getPlayersByPosition(long playerId) {

		List<Long> members = new ArrayList<Long>();

		Union union = getUnionByPlayerId(playerId);

		if (union != null) {
			for (UnionMember member : union.getMembers()) {
				if (member.getPlayerId() != playerId) {
					members.add(member.getPlayerId());
				}
			}
		}
		return members;
	}

	/**
	 * 增加资源
	 * 
	 * @param playerId
	 * @param unionResource
	 * @param asset
	 * @param eventType
	 */
	public void addResource(long playerId, int unionResource, int asset, int dedicate, int eventType) {

		Union union = Context.getUnionServer().getUnionByPlayerId(playerId);
		if (union == null) {
			return;
		}

		synchronized (union) {

			if (unionResource > 0) {
				UnionConfig config = unionManager.getUnionConfigByLevel(union.getLevel());
				int capacity = union.getResource() + unionResource;
				capacity = capacity > config.getCapacity() ? config.getCapacity() : capacity;
				union.setResource(capacity);
			}

			if (dedicate > 0) {
				UnionMember member = union.getMember(playerId);
				member.setResource(member.getResource() + dedicate);
				member.setHissource(member.getHissource() + dedicate);
				unionManager.updateMemberResource(member);
			}

			if (asset > 0) {
				union.alertAsset(asset);
			}
			
			if (asset > 0 || unionResource > 0) {
				Player player = Context.getPlayerServer().getByPlayerId(playerId);
				addAssetLog(union.getId(), player.getPlayerId(), player.getPlayerName(), asset, unionResource, eventType);
			}
			unionManager.addResource(union);
		}
	}

	public void addAssetLog(int unionId, long playerId, String playerName, int asset, int resource, int eventType) {
		if (asset != 0 || resource != 0) {
			unionManager.addAssetLog(unionId, playerId, playerName, asset, resource, eventType);
		}
	}
}
